package io.renren.modules.commodity.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.commodity.dao.CommodityImgDao;
import io.renren.modules.commodity.entity.CommodityImgEntity;
import io.renren.modules.commodity.service.CommodityImgService;


@Service("commodityImgService")
public class CommodityImgServiceImpl extends ServiceImpl<CommodityImgDao, CommodityImgEntity> implements CommodityImgService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<CommodityImgEntity> queryWrapper = new QueryWrapper<>();
        if (Query.STATUS_ACTIVED.equals(params.get(Query.STATUS))) {
            queryWrapper.eq("status", 1);
        }

        IPage<CommodityImgEntity> page = this.page(
                new Query<CommodityImgEntity>().getPage(params),
                queryWrapper
        );


        return new PageUtils(page);
    }

    @Override
    public List<String> queryImgUrlsByCommodityId(Long id) {

        List<CommodityImgEntity> list = baseMapper.selectList(new QueryWrapper<CommodityImgEntity>().eq("commodity_id", id));
        if (list.isEmpty()) {
            return null;
        }
        List<String> result = new ArrayList<>();
        for (CommodityImgEntity entity : list) {
            result.add(entity.getImgUrl());
        }

        return result;
    }


}